1
/****************************** Module Header ******************************\
2 * Module Name: MainForm.cs
3 * Project: CSGDIPlusManipulateImage
4 * Copyright (c) Microsoft Corporation.
6 * This is the main form of this application. It is used to initialize the UI and
9 * This source is subject to the Microsoft Public License.
10 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
11 * All other rights reserved.
13 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
14 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
16 \***************************************************************************/
20 using System
.Windows
.Forms
;
21 using System
.Drawing
.Drawing2D
;
23 namespace CSGDIPlusManipulateImage
25 public partial class MainForm
: Form
29 ImageManipulator imgManipulator
= null;
31 Point adjustment
= Point
.Empty
;
35 InitializeComponent();
37 // Load a bitmap from a local file.
38 Bitmap img
= new Bitmap("OneCodeIcon.png");
40 // Initialize the bmpEx.
41 imgManipulator
= new ImageManipulator(img
);
43 // Add all the InterpolationMode to the combobox.
44 for (int i
= 0; i
<= 7; i
++)
46 cmbInterpolationMode
.Items
.Add((InterpolationMode
)(i
));
49 cmbInterpolationMode
.SelectedIndex
= 0;
54 /// Handle the click event of the buttons btnRotateLeft, btnRotateRight,
55 /// btnFlipVertical and btnFlipHorizontal.
57 private void btnRotateFlip_Click(object sender
, EventArgs e
)
59 Button rotateFlipButton
= sender
as Button
;
61 if (rotateFlipButton
== null)
66 RotateFlipType rotateFlipType
= RotateFlipType
.RotateNoneFlipNone
;
68 switch (rotateFlipButton
.Name
)
71 rotateFlipType
= RotateFlipType
.Rotate270FlipNone
;
73 case "btnRotateRight":
74 rotateFlipType
= RotateFlipType
.Rotate90FlipNone
;
76 case "btnFlipVertical":
77 rotateFlipType
= RotateFlipType
.RotateNoneFlipY
;
79 case "btnFlipHorizontal":
80 rotateFlipType
= RotateFlipType
.RotateNoneFlipX
;
84 // Rotate or flip the image.
85 imgManipulator
.RotateFlip(rotateFlipType
);
87 // Redraw the pnlImage.
88 imgManipulator
.DrawControl(this.pnlImage
, adjustment
, pen
);
92 /// Handle the click event of the button btnRotateAngle.
94 private void btnRotateAngle_Click(object sender
, EventArgs e
)
98 // Verify the input value.
99 float.TryParse(tbRotateAngle
.Text
, out angle
);
101 if (angle
> 0 && angle
< 360)
103 // Rotate or flip the image.
104 imgManipulator
.RotateImg(angle
);
106 // Redraw the pnlImage.
107 imgManipulator
.DrawControl(this.pnlImage
, adjustment
, pen
);
113 /// Handle the click event of the buttons btnMoveUp, btnMoveDown,
114 /// btnMoveLeft and btnMoveRight.
116 private void btnMove_Click(object sender
, EventArgs e
)
118 Button moveButton
= sender
as Button
;
119 if (moveButton
== null)
127 switch (moveButton
.Name
)
142 adjustment
= new Point(adjustment
.X
+ x
, adjustment
.Y
+ y
);
144 // Redraw the pnlImage.
145 imgManipulator
.DrawControl(this.pnlImage
, adjustment
, pen
);
150 /// Draw the image on the pnlImage when it is painted.
152 private void pnlImage_Paint(object sender
, PaintEventArgs e
)
155 // Draw the pnlImage for the first time..
156 imgManipulator
.DrawControl(this.pnlImage
, adjustment
, pen
);
161 /// Handle the click event of the buttons btnAmplify and btnMicrify.
163 private void btnAmplify_Click(object sender
, EventArgs e
)
165 Button btnScale
= sender
as Button
;
166 if (btnScale
.Name
== "btnAmplify")
168 imgManipulator
.Scale(2, 2, (InterpolationMode
)cmbInterpolationMode
.SelectedItem
);
172 imgManipulator
.Scale(0.5f
, 0.5f
, (InterpolationMode
)cmbInterpolationMode
.SelectedItem
);
175 // Redraw the pnlImage.
176 imgManipulator
.DrawControl(this.pnlImage
, adjustment
, pen
);
181 /// andle the click event of the buttons btnSkewLeft and btnSkewRight.
183 /// <param name="sender"></param>
184 /// <param name="e"></param>
185 private void btnSkew_Click(object sender
, EventArgs e
)
187 Button btnSkew
= sender
as Button
;
192 imgManipulator
.Skew(-10);
195 imgManipulator
.Skew( 10);
199 // Redraw the pnlImage.
200 imgManipulator
.DrawControl(this.pnlImage
, adjustment
, pen
);
207 private void btnReset_Click(object sender
, EventArgs e
)
210 // Dispose the bmpEx.
211 imgManipulator
.Dispose();
213 // Load a bitmap from a local file.
214 Bitmap img
= new Bitmap("OneCodeIcon.png");
216 // Initialize the bmpEx.
217 imgManipulator
= new ImageManipulator(img
);
219 // Redraw the pnlImage.
220 imgManipulator
.DrawControl(this.pnlImage
, adjustment
, pen
);
225 /// Handle the CheckedChanged event of the checkbox chkDrawBounds.
227 private void chkDrawBounds_CheckedChanged(object sender
, EventArgs e
)
230 // If the pen is not null, draw the bounds of the image.
231 if (chkDrawBounds
.Checked
)
240 // Redraw the pnlImage.
241 imgManipulator
.DrawControl(this.pnlImage
, adjustment
, pen
);